<?php

require_once "class.db.php";

class sanpham extends db {
    /* SAN PHAM */

    function SanPham_Them(&$loi) {

        $thanhcong = true;
        $idTL=(int) $_POST['idTL'];        
        $loai_id = (int) $_POST['loai_id'];
        $kg_id = (int) $_POST['kg_id'];

        $ma_sp = $this->processData($_POST['ma_sp']);

        $ten_sp = $this->processData($_POST['ten_sp']);
        $ten_sp_kd = $this->processData($_POST['ten_sp_kd']);

        $gia = $this->processData($_POST['gia']);
        $khuyen_mai = $this->processData($_POST['khuyen_mai']);

        $ngay = strtotime($this->processData($_POST['ngay']));

        echo $hinh_dai_dien = $this->processData($_POST['UrlHinh']);
		//$hinh_anh= $this->processData($_POST['hinh_dai_dien_vuong']);
        $hinh_anh = $this->processData($_POST['hinh_anh']);
        //$hinh_anh_vuong = $this->processData($_POST['hinh_anh_vuong']);

        $mo_ta = $this->processData($_POST['mo_ta']);
		$chi_tiet = $_POST['chi_tiet'];

        $tags = rtrim(trim($_POST['tags']), ";");
        $tags = str_replace(',', ';', $tags);
        
        $arrTagVi = explode("; ", $tags);      

        $arrChat = $_POST['chat_id'];
        $arrMau = $_POST['mau'];
		$arrKieu = $_POST['kieu_id'];

        $loai_id = $_POST['loai_id'];
        $arrSize = $_POST['size'];


        if ($ten_sp_kd == "")
            $ten_sp_kd = $this->changeTitle($ten_sp);
       
        if ($thanhcong == false) {
            return $thanhcong;
        } else {
            echo $sql = "INSERT INTO sanpham
					VALUES(NULL,'$ma_sp','$ten_sp','$ten_sp_kd','$mo_ta','$chi_tiet','$hinh_dai_dien',
                                            '$hinh_anh','$gia','$khuyen_mai','$ngay','$loai_id','$idTL','$kg_id')";
            mysql_query($sql) or die(mysql_error() . $sql);
            $sp_id = mysql_insert_id();
            foreach ($arrMau as $maus) {
                mysql_query("INSERT INTO sp_mau VALUES($sp_id,$maus,$loai_id)");
            }
            foreach ($arrSize as $sizes) {
                mysql_query("INSERT INTO sp_size VALUES($sp_id,$sizes)");
            }
            foreach ($arrChat as $chat_id) {
                mysql_query("INSERT INTO sp_chat VALUES($chat_id,$sp_id)");
            }
			foreach ($arrKieu as $kieu_id) {
                mysql_query("INSERT INTO sp_kieu VALUES($kieu_id,$sp_id)");
            }

            /* Tags */
            if (!empty($arrTagVi)) {
                foreach ($arrTagVi as $tag) {
                    $tag_id = $this->checkTagTonTai($tag);
                    $this->addTagToArticle($sp_id, $tag_id);
                }
            }            
        }
        return $thanhcong;
    }

    function SanPham_Sua($sp_id, &$loi) {      
        $thanhcong = true;
         $idTL=(int) $_POST['idTL'];  
        $loai_id = (int) $_POST['loai_id'];        
        $kg_id = (int) $_POST['kg_id'];
        
        $ma_sp = $this->processData($_POST['ma_sp']);

        $ten_sp = $this->processData($_POST['ten_sp']);
        $ten_sp_kd = $this->processData($_POST['ten_sp_kd']);

        $gia = $this->processData($_POST['gia']);
        $khuyen_mai = $this->processData($_POST['khuyen_mai']);

        $ngay = strtotime($this->processData($_POST['ngay']));

        $hinh_dai_dien = str_replace('../','',$this->processData($_POST['UrlHinh']));
		$hinh_anh= str_replace('../','',$this->processData($_POST['hinh_anh']));
        $hinh_anh_cu = $this->processData($_POST['hinh_anh_cu']);
        $hinh_anh = $this->processData($_POST['hinh_anh']);
        if ($hinh_anh != '') {
            if ($hinh_anh_cu != '') {
                $hinh_anh = $hinh_anh_cu . ";" . $hinh_anh;
            } else {
                $hinh_anh = $hinh_anh;
            }
        } else {
            $hinh_anh = $hinh_anh_cu;
        }
        
        /*$hinh_anh_cu_vuong = $this->processData($_POST['hinh_anh_cu_vuong']);
        $hinh_anh_vuong = $this->processData($_POST['hinh_anh_vuong']);
        if ($hinh_anh_vuong != '') {
            if ($hinh_anh_cu_vuong != '') {
                $hinh_anh_vuong = $hinh_anh_cu_vuong . ";" . $hinh_anh_vuong;
            } else {
                $hinh_anh_vuong = $hinh_anh_vuong;
            }
        } else {
            $hinh_anh_vuong = $hinh_anh_cu_vuong;
        }*/

        $mo_ta = $this->processData($_POST['mo_ta']);
		$chi_tiet = $_POST['chi_tiet'];
   
        $tags = rtrim(trim($_POST['tags']), ";");
        $tags = str_replace(',', ';', $tags);
       
        if ($tags != '')
            $arrTagVi = explode("; ", $tags);       

        $arrChat = $_POST['chat_id'];
		$arrKieu = $_POST['kieu_id'];
        $arrMau = $_POST['mau'];       
        $arrSize = $_POST['size'];

        if ($ten_sp_kd == "")
            $ten_sp_kd = $this->changeTitle($ten_sp);    

        if ($thanhcong == false) {
            return $thanhcong;
        } else {
           $sql = "UPDATE sanpham SET
					ma_sp = '$ma_sp',ten_sp = '$ten_sp',
					ten_sp_kd = '$ten_sp_kd',
					mo_ta ='$mo_ta',
					chi_tiet = '$chi_tiet',
					hinh_dai_dien = '$hinh_dai_dien',
					hinh_anh= '$hinh_anh',
					gia = '$gia',                                          
					khuyen_mai = '$khuyen_mai',ngay = '$ngay',
                                        loai_id = $loai_id,idTL=$idTL,kg_id = $kg_id
					WHERE sp_id = $sp_id";
            mysql_query($sql) or die(mysql_error() . $sql);


            if (mysql_query("DELETE FROM sp_mau WHERE sp_id = $sp_id")) {
                foreach ($arrMau as $maus) {
                    mysql_query("INSERT INTO sp_mau VALUES($sp_id,$maus,$loai_id)");
                }
            }
            if (mysql_query("DELETE FROM sp_size WHERE sp_id = $sp_id")) {
                foreach ($arrSize as $sizes) {
                    mysql_query("INSERT INTO sp_size VALUES($sp_id,$sizes)");
                }
            }            
            if (mysql_query("DELETE FROM sp_chat WHERE sp_id = $sp_id")) {
                foreach ($arrChat as $chat_id) {
                    mysql_query("INSERT INTO sp_chat VALUES($chat_id,$sp_id)");
                }
            }
			if (mysql_query("DELETE FROM sp_kieu WHERE sp_id = $sp_id")) {
                foreach ($arrKieu as $kieu_id) {
                    mysql_query("INSERT INTO sp_kieu VALUES($kieu_id,$sp_id)");
                }
            }
            /* Tags */
            if (!empty($arrTagVi)) {
                mysql_query("DELETE FROM sp_tag WHERE sp_id = $sp_id");
                foreach ($arrTagVi as $tag_v) {
                    $tag_id = $this->checkTagTonTai($tag_v);
                    $this->addTagToArticle($sp_id, $tag_id);
                }
            } else {
                mysql_query("DELETE FROM sp_tag WHERE sp_id = $sp_id");
            }           
        }
        return $thanhcong;
    }

    function getDetailSP($sp_id) {
        $sql = "SELECT * 
				FROM sanpham
				WHERE sp_id = $sp_id";
        $rs = mysql_query($sql) or die(mysql_error());
        return $rs;
    }
    
    function SanPham_List() {
        $sql = "SELECT * FROM sanpham	";
        $rs = mysql_query($sql) or die(mysql_error());
        return $rs;
    }
    function SanPham_Dac_Trung() {
        $sql = "SELECT * FROM sanpham WHERE loai_id=5	";
        $rs = mysql_query($sql) or die(mysql_error());
        return $rs;
    }
    function SanPham_PhuKien() {
        $sql = "SELECT * FROM sanpham WHERE idTL=4	";
        $rs = mysql_query($sql) or die(mysql_error());
        return $rs;
    }
    function SanPham_Couple() {
        $sql = "SELECT * FROM sanpham WHERE idTL=3	";
        $rs = mysql_query($sql) or die(mysql_error());
        return $rs;
    }
    function SanPham_List_TheoTheLoai($idTL = -1 , $tukhoa = '', $limit = -1, $offset = -1) {
        $sql = "SELECT sp_id,ten_sp,ten_sp_kd,hinh_dai_dien,gia FROM sanpham 
					WHERE idTL = $idTL OR $idTL=-1 ";
        if ($tukhoa != "")
            $sql.=" AND ten_sp LIKE '%$tukhoa%' ";
        $sql.="	ORDER BY sp_id DESC ";
        if ($limit > 0 && $offset >= 0)
            $sql.= " LIMIT $offset,$limit";

        $rs = mysql_query($sql) or die(mysql_error());
        return $rs;
    }
    function SanPham_List_TheoLoai($idLoai = -1 , $tukhoa = '', $limit = -1, $offset = -1) {
        $sql = "SELECT * FROM sanpham 
					WHERE loai_id = $idLoai OR $idLoai=-1 ";
        if ($tukhoa != "")
            $sql.=" AND ten_sp LIKE '%$tukhoa%' ";
        $sql.="	ORDER BY sp_id DESC ";
        if ($limit > 0 && $offset >= 0)
            $sql.= " LIMIT $offset,$limit";

        $rs = mysql_query($sql) or die(mysql_error());
        return $rs;
    }

    function getSPNext($sp_id, $loai_id) {
        $array = array();
        $sql = "SELECT MIN(sp_id) as sp_id FROM sp_loai WHERE sp_id > $sp_id AND loai_id IN('$loai_id')";
        $rs = mysql_query($sql);
        $kq = mysql_fetch_assoc($rs);
        if ($kq['sp_id'] == NULL) {
            return $array;
        } else {
            $sp = $this->getDetailSP($kq['sp_id']);
            $array = mysql_fetch_assoc($sp);
            return $array;
        }
    }

    function getSPPrev($sp_id, $loai_id) {
        $array = array();
        $sql = "SELECT MAX(sp_id) as sp_id FROM sp_loai WHERE sp_id < $sp_id AND loai_id IN('$loai_id')";
        $rs = mysql_query($sql);
        $kq = mysql_fetch_assoc($rs);
        if ($kq['sp_id'] == NULL) {
            return $array;
        } else {
            $sp = $this->getDetailSP($kq['sp_id']);
            $array = mysql_fetch_assoc($sp);
            return $array;
        }
    }

    function getLoaiId($sp_id) {
        $sql = "SELECT loai_id FROM sp_loai WHERE sp_id = $sp_id";
        $rs = mysql_query($sql);
        $strLoai_id = '';
        while ($row = mysql_fetch_assoc($rs)) {
            $strLoai_id .= $row['loai_id'] . ",";
        }
        $strLoai_id = rtrim($strLoai_id, ",");
        return $strLoai_id;
    }

    function getLoaiIdByProductId($sp_id) {
        $arr = array();
        $sql = "SELECT loai_id FROM sp_loai WHERE sp_id = $sp_id";
        $rs = mysql_query($sql);
        while ($row = mysql_fetch_assoc($rs)) {
            $arr[] = $row['loai_id'];
        }
        return $arr;
    }

    function getKieuIdByProductId($sp_id) {
        $arr = array();
        $sql = "SELECT kieu_id FROM sp_kieu WHERE sp_id = $sp_id";
        $rs = mysql_query($sql);
        while ($row = mysql_fetch_assoc($rs)) {
            $arr[] = $row['kieu_id'];
        }
        return $arr;
    }

    function getChatIdByProductId($sp_id) {
        $arr = array();
        $sql = "SELECT chat_id FROM sp_chat WHERE sp_id = $sp_id";
        $rs = mysql_query($sql);
        while ($row = mysql_fetch_assoc($rs)) {
            $arr[] = $row['chat_id'];
        }
        return $arr;
    }

    function getTagsByProductId($sp_id) {
        $sql = "SELECT tag_id FROM sp_tag WHERE sp_id = $sp_id ";
        $rs = mysql_query($sql);
        return $rs;
    }

    function getTagsOfProductId($sp_id) {
        $arr = array();
        $sql = "SELECT tag_id FROM sp_tag WHERE sp_id = $sp_id ";
        $rs = mysql_query($sql);
        while ($row = mysql_fetch_assoc($rs)) {
            $arr[] = $row['tag_id'];
        }
        return $arr;
    }

    function getDetailTag($tag_id) {
        $sql = "SELECT * FROM tag WHERE tag_id = $tag_id";
        $rs = mysql_query($sql);
        return $rs;
    }

    function getSizeIdByProductId($sp_id) {
        $arr = array();
        $sql = "SELECT size_id FROM sp_size WHERE sp_id = $sp_id";
        $rs = mysql_query($sql);
        while ($row = mysql_fetch_assoc($rs)) {
            $arr[] = $row['size_id'];
        }
        return $arr;
    }

    function getMauIdByProductId($sp_id) {
        $arr = array();
        $sql = "SELECT mau_id FROM sp_mau WHERE sp_id = $sp_id";
        $rs = mysql_query($sql);
        while ($row = mysql_fetch_assoc($rs)) {
            $arr[] = $row['mau_id'];
        }
        return $arr;
    }
    
    function List_SPKhuyenMai() {
        $sql = "SELECT * FROM sanpham WHERE khuyen_mai != 0";
        $rs = mysql_query($sql);
        return $rs;
    }

}

?>